/**
 * 
 */
package org.rollinitiative.d20.encounter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rollinitiative.d20.combat.Allegiance;
import org.rollinitiative.d20.combat.Combatant;
import org.rollinitiative.d20.entity.Actor;

/**
 * The Roster class manages combatants participating in an encounter.
 * 
 * @author bebopjmm
 * 
 */
public class Roster
{
    static final Log LOG_ = LogFactory.getLog(Roster.class);
    
    /**
     * 
     */
    Hashtable<Actor, Combatant> combatants_;
    
    Hashtable <Allegiance, ArrayList<Combatant>> allegianceMap_;
    
    public Roster()
    {
        combatants_ = new Hashtable<Actor, Combatant>();
        allegianceMap_ = new Hashtable<Allegiance, ArrayList<Combatant>>();
        allegianceMap_.put(Allegiance.PARTY, new ArrayList<Combatant>());
        allegianceMap_.put(Allegiance.NEUTRAL, new ArrayList<Combatant>());
        allegianceMap_.put(Allegiance.HOSTILE, new ArrayList<Combatant>());
    }
    
    public synchronized void add(Combatant combatant)
    {
        combatants_.put(combatant.getActor(), combatant);
        ArrayList<Combatant> allies = allegianceMap_.get(combatant.getAllegiance());
        if (allies == null) {
            LOG_.error("Combatant list NULL for allegiance: " + combatant.getAllegiance());
            return;
        }
        allies.add(combatant);
    }
    
    public synchronized void remove(Combatant combatant)
    {
        combatants_.remove(combatant.getActor());
        ArrayList<Combatant> allies = allegianceMap_.get(combatant.getAllegiance());
        if (allies == null) {
            LOG_.error("Combatant list NULL for allegiance: " + combatant.getAllegiance());
            return;
        }
        allies.remove(combatant);
    }
    
    public boolean isParticipating(Actor actor)
    {
        return combatants_.containsKey(actor);
    }
    
    public int getNumberCombatants()
    {
        return combatants_.size();
    }
    
    public Collection<Combatant> getCombatants()
    {
        return  combatants_.values();
    }
    
    public Combatant getCombatant(Actor actor)
    {
        return combatants_.get(actor);
    }
}
